#include "Analyzer.h"
/*Costructor
 * also initialize UdpClient*/
Analyzer::Analyzer() {
rtp_ccc = new ContinuityCounterController(0xffff);
last_packet_toa = 0;
last_rtp_timestamp = 0;
}

bool Analyzer::process(UdpDatagram * udp_d) {
	static bool ts_packet_cc_error;
	static int ts_pid;
	static int ts_cc;
	char * payload;
	int payload_length;
	fprintf(stdout,"Packet nbr: %i\n",received_packet);

	gui->cleantxt();
	gchar * stuff = g_strdup_printf("Packet nbr: %d\n", received_packet);
	gui->print(stuff);
	g_free(stuff);



	//Detect if we have an RTP or TS transmissione

	if (RtpPacket::isRTPPacket(udp_d->buffer, udp_d->length)) {
		RtpPacket * rtp_p = new RtpPacket(udp_d->buffer,udp_d->length);
		rtp_ccc->push(rtp_p->getSequenceNbr());


		int diff_timestamp = (rtp_p->getTimestamp() -  last_rtp_timestamp) / 0.09f;
		int diff_toa = udp_d->toa - last_packet_toa;

		last_rtp_timestamp = rtp_p->getTimestamp();
		last_packet_toa = udp_d->toa;

		this->ujitter = (diff_toa*1.0f - diff_timestamp);

		fprintf(stdout, "JITTER: ts:%i toa:%i  %f diff:%f \n",diff_timestamp,diff_toa,(diff_toa*1.0f) / (diff_timestamp*1.0f),(diff_toa*1.0f - diff_timestamp));

		gchar * stuff = g_strdup_printf("JITTER: ts:%i toa:%i  %f diff:%f \n", diff_timestamp,diff_toa,(diff_toa*1.0f) / (diff_timestamp*1.0f),(diff_toa*1.0f - diff_timestamp));
		gui->print(stuff);
		g_free(stuff);

	}



	TsPacket ** ts_packet_array = new TsPacket*[10];
	int ts_nbr = 0;

	TsPacket::demuxTS(udp_d->buffer, udp_d->length, ts_packet_array, ts_nbr);

	for (int i = 0; i < ts_nbr; i++) {

		ts_pid = ts_packet_array[i]->getPID();
		ts_cc = ts_packet_array[i]->getContinuityCounter();

		fprintf(stdout, "-> TS , PID = %x , CC = %i \n",ts_pid,ts_cc);
		gchar * stuff = g_strdup_printf( "-> TS , PID = %x , CC = %i \n",ts_pid,ts_cc);
		gui->print(stuff);
		g_free(stuff);

		if (ts_packet_array[i]->getPayload(payload,payload_length)){
			//fprintf(stdout, "-> TS payload start:%u length:%i firstbyte: %x %x %x %x \n",payload-ts_packet_array[i]->buffer,payload_length,payload[0]&0xff,payload[1]&0xff,payload[2]&0xff,payload[3]&0xff);
		}

		if (ts_ccc.find(ts_pid) == ts_ccc.end()) {
			ts_ccc[ts_pid] = new ContinuityCounterController(16);
			ts_ccc[ts_pid]->last_cc = ts_cc;
			fprintf(stdout, "Found one  PID: %x, cc = %i\n",ts_pid,ts_cc);

			gchar * stuff = g_strdup_printf("Found one  PID: %x, cc = %i\n",ts_pid,ts_cc);
			gui->print(stuff);
			g_free(stuff);


		} else {
			ts_packet_cc_error = ts_ccc[ts_pid]->push(ts_cc);
			if (!ts_packet_cc_error) {
				fprintf(stderr, "Countinuity Counter Error PID: %x, last_cc = %i, cc = %i\n",ts_pid,ts_ccc[ts_pid]->last_cc,ts_cc);

				gchar * stuff = g_strdup_printf("Countinuity Counter Error PID: %x, last_cc = %i, cc = %i\n",ts_pid,ts_ccc[ts_pid]->last_cc,ts_cc);
				gui->print(stuff);
				g_free(stuff);

				ts_ccc[ts_pid]->last_cc = ts_cc;
				nbr_of_packet_loss++;
			}
		}

	}

	packet_loss_rate = nbr_of_packet_loss*1.0f / received_packet;

	delete[] ts_packet_array;

	return true;
}
